package mypackage;

import java.util.Timer;
import java.util.TimerTask;

import com.dg.admodule.AdModule;
import com.dg.admodule.adbutton.AdButton;
import com.dg.admodule.adbutton.SmaatoAdField;
import com.smaato.SmaatoAPICallback;
import com.smaato.SmaatoAd;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen implements SmaatoAPICallback
{
	
	SmaatoAdField baner;
	
    public MyScreen()
    {        
        // Set the displayed title of the screen       
        setTitle("MyTitle");
        baner = new SmaatoAdField(null);
        ButtonField btn = new ButtonField("button", ButtonField.CONSUME_CLICK);
        btn.setChangeListener(new FieldChangeListener() {
			
			public void fieldChanged(Field field, int context) {
				//runAd();
			}
		});
        add(btn);
        add(baner);
        UiApplication.getUiApplication().invokeLater(new Runnable() {
			
			public void run() {
				runAd();
			}
		});
    }
	public void getAdNotifySuccess() {
		AdModule.getInstance().setIRequestOutstanding(false);
		
		// Success, calling showAd is now safe
		SmaatoAd ad = AdModule.getInstance().getSmaatoApi().showAd();
		
		// Pass the SmaatoAdField the Ad to display
		baner.setSmaatoAd(ad);
	}
	public void getAdNotifyError(int errorCode, String errorString) {
		// TODO Auto-generated method stub
		
	}
	
	private void runAd(){
		Timer t = new Timer();
		t.schedule(new TimerTask() {
			
			public void run() {
				AdModule.getInstance().getSmaatoApi().getAd(MyScreen.this);
			}
		}, 0, 10000);
	}
	
	public boolean onClose() {
		if(baner.isAnimated()){
			baner.stop();
		}
		
		return super.onClose();
	}
}
